package org.codeart.common.util;

import lombok.Data;

@Data
public class R<T> {

    private Integer code = 200;

    private String msg = "";

    private T data;

    public static final String SUCCESS = "操作成功";

    public static final String FAILURE = "操作失败";

    public R() {

    }

    public R(T data) {
        this.data = data;
    }

    public R(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> R<T> ok() {
        return ok(HttpStatus.HTTP_OK, SUCCESS, null);
    }

    public static <T> R<T> ok(Integer code, String msg, T data) {
        R<T> r = new R<>();
        r.setCode(code);
        r.setMsg(msg);
        r.setData(data);
        return r;
    }

    public static <T> R<T> ok(T data) {
        return ok(HttpStatus.HTTP_OK, SUCCESS, data);
    }

    public static <T> R<T> fail(Integer code, String msg) {
        return ok(code, msg, null);
    }

    public static <T> R<T> fail(String msg) {
        return ok(HttpStatus.HTTP_INTERNAL_ERROR, msg, null);
    }

    public static <T> R<T> fail() {
        return ok(HttpStatus.HTTP_INTERNAL_ERROR, FAILURE, null);
    }
}
